﻿using System;
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Plugins;
using Xenta.Structs;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class OrderAttributeTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        protected override IDictionary<DependencyInfo, IDependency> Dependencies
        {
            get
            {
                var components = new Dictionary<DependencyInfo, IDependency>();
                var salesModel = new Mock<ISalesModel>();
                components.Add(new DependencyInfo
                {
                    ID = "c616d39a-88bf-42b8-947a-d7a752abc176"
                }, salesModel.Object);
                var shippingMethod = new Mock<IShippingMethod>();
                shippingMethod.Setup(x => x.CanShipFrom(It.IsAny<IAddress>()))
                    .Returns(true);
                shippingMethod.Setup(x => x.CanShipTo(It.IsAny<IAddress>()))
                    .Returns(true);
                components.Add(new DependencyInfo
                {
                    ID = "68c82a8b-add4-4a1e-9e74-52da6a02b103"
                }, shippingMethod.Object);
                return components;
            }
        }

        [TestCase("key", "value")]
        public void Can_CRUD(string key, string value)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var customer = Processor.Execute(new CreateCustomer
            {
                AccountID = account.EntityID
            }).Result;
            var customerAddr = Processor.Execute(new CreateCustomerAddress
            {
                HolderID = customer.EntityID,
                CountryID = country.EntityID,
                RegionID = localizationProfile.Region.EntityID
            }).Result;
            var seller = Processor.Execute(new CreateSeller
            {
                AccountID = account.EntityID
            }).Result;
            var sellerAddr = Processor.Execute(new CreateSellerAddress
            {
                HolderID = seller.EntityID,
                CountryID = country.EntityID,
                RegionID = localizationProfile.Region.EntityID
            }).Result;
            var order = Processor.Execute(new CreateOrder
            {
                CustomerID = customer.EntityID,
                SellerID = seller.EntityID,
                BillingAddressID = customerAddr.EntityID,
                ShippingAddressID = customerAddr.EntityID,
                SellerAddressID = sellerAddr.EntityID,
                SalesModelID = Guid.Parse("c616d39a-88bf-42b8-947a-d7a752abc176"),
                ShippingMethodID = Guid.Parse("68c82a8b-add4-4a1e-9e74-52da6a02b103"),
                HandlingFee = Decimal.Zero,
                DiscountAmount = Decimal.Zero,
                ShippingPrice = Decimal.Zero,
                Subtotal = Decimal.Zero,
                TaxAmount = Decimal.Zero,
                Total = Decimal.Zero,
                Flags = OrderFlags.Pending
            }).Result;

            #endregion

            var attribute = Processor.Execute(new CreateOrderAttribute
            {
                HolderID = order.EntityID,
                Key = key,
                Value = value
            }).Result;

            Assert.NotNull(attribute);
            attribute.Key.AreEqualIgnoringCase(key);
            attribute.Value.AreEqualIgnoringCase(value);
            attribute.AreEqual(Processor.Execute(new GetOrderAttribute
            {
                EntityID = attribute.EntityID
            }).Result);
            attribute.AreEqual(Processor.Execute(new GetOrderAttributeByKey
            {
                HolderID = order.EntityID,
                Key = key
            }).Result);

            value = value.Reverse();
            var upd_attribute = Processor.Execute(new UpdateOrderAttribute
            {
                EntityID = attribute.EntityID,
                Value = value
            }).Result;

            Assert.NotNull(upd_attribute);
            upd_attribute.AreEqual(attribute);
            upd_attribute.Key.AreEqualIgnoringCase(key);
            upd_attribute.Value.AreEqualIgnoringCase(value);

            Processor.Execute(new DeleteOrderAttribute
            {
                EntityID = attribute.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Key_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateOrderAttribute
            {
                Key = String.Empty
            }));
        }
    }
}
